package com.jiang.jixiang.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;


import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.jiang.jixiang.R;
import com.jiang.jixiang.databinding.ActivityLanguareBinding;
import com.jiang.jixiang.tools.PubDiaUtils;
import com.jiang.jixiang.ui.adapter.LanguageAdapter;
import com.ws.universal.tools.Constants;
import com.ws.universal.tools.base.BaseActivity;
import com.ws.universal.tools.base.LanguageBean;
import com.ws.universal.tools.utils.CacheUtils;
import com.ws.universal.tools.utils.LanguageUtils;
import com.ws.universal.tools.utils.LogcatUtils;

import java.util.List;

public class LanguageActivity extends BaseActivity {
    private static List<LanguageBean> dataList;
    private ActivityLanguareBinding binding;
    private LanguageAdapter languageAdapter;
    private int indexPostion;
    private int type;
    private Intent intent;

    @Override
    protected void initCreate(Bundle savedInstanceState) {
        binding = ActivityLanguareBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        intent = getIntent();
        type = intent.getIntExtra("type", 2);


    }

    @Override
    protected void initView() {
        if(type==1){
            binding.llLogo.setVisibility(View.VISIBLE);
        }else {
            binding.llLogo.setVisibility(View.GONE);
        }
        CacheUtils.setBoolean(Constants.isFristInstall, true);
        binding.inTitle.titleTextTv.setText(R.string.language);
        binding.inTitle.titleLeftBtn.setOnClickListener(view -> {
            finish();
        });
        languageAdapter = new LanguageAdapter(type);
       /* binding.btnGo.setOnClickListener(v -> {
            LanguageUtils.applyLanguage(languageAdapter.getData().get(indexPostion).getLocale(), getActivity(), MainActivity.class);
        });*/


        LinearLayoutManager manager = new LinearLayoutManager(getContext());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        binding.rcyData.setLayoutManager(manager);
        binding.rcyData.setAdapter(languageAdapter);
        dataList = LanguageUtils.getDataList();
        LogcatUtils.e(" LanguageActivity Cache " + CacheUtils.getString(Constants.langCode));
        for (int i = 0; i < dataList.size(); i++) {
            if (CacheUtils.getString(Constants.langCode).equals(dataList.get(i).getLanguageCode())) {
                indexPostion = i;
                CacheUtils.setString(Constants.langCode, dataList.get(i).getLanguageCode());
                dataList.get(i).setChoose(true);
            } else {
                dataList.get(i).setChoose(false);
            }

            LogcatUtils.e(" LanguageActivity  getLanguageCode " + dataList.get(i).getLanguageCode());

        }
        languageAdapter.setList(dataList);

        languageAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
                indexPostion = position;
                if (!CacheUtils.getBoolean(Constants.isFristInstall)) {
                    toggleSelection();
                } else {
                    PubDiaUtils.getInstance().showTwoBtnDialog(getContext(), "", getResources().getString(R.string.language_change), getResources().getString(R.string.cancel), getResources().getString(R.string.sure), new PubDiaUtils.PublicDiaologCallback() {
                        @Override
                        public void onConfirm() {
                            choose();
                        }

                        @Override
                        public void onCancel() {
                            //choose(position);
                        }
                    });
                }


            }
        });


    }

    private void toggleSelection() {
        //单选
        for (int i = 0; i < languageAdapter.getData().size(); i++) {
            if (i == indexPostion) {
                CacheUtils.setString(Constants.langCode, languageAdapter.getData().get(i).getLanguageCode());
                languageAdapter.getData().get(indexPostion).setChoose(true);
            } else {
                languageAdapter.getData().get(i).setChoose(false);
            }
        }

        languageAdapter.notifyDataSetChanged();

    }

    private void choose() {
        //单选
        for (int i = 0; i < languageAdapter.getData().size(); i++) {
            if (i == indexPostion) {
                languageAdapter.getData().get(i).setChoose(true);
                CacheUtils.setString(Constants.langCode, languageAdapter.getData().get(i).getLanguageCode());
            } else {
                languageAdapter.getData().get(i).setChoose(false);
            }
        }
        languageAdapter.notifyDataSetChanged();
        LanguageUtils.applyLanguage(languageAdapter.getData().get(indexPostion).getLocale(), getActivity(), MainActivity.class);
        finish();

    }

    @Override
    protected void FinishDesTroy() {

    }
}
